<?php

namespace Upfor\UpforPHP\View;

use Smarty;
use ErrorException;
use Upfor\UpforPHP\View\DefaultView;

/**
 * Smarty
 */
class SmartyView extends DefaultView {

    /**
     * Persistent instance of the Parser object
     *
     * @var Smarty
     */
    private $parserInstance = null;

    /**
     * Render Template
     *
     * This method will output the rendered template content
     *
     * @param  string $template The template pathname, relative to the template base directory
     * @return string           The rendered template string
     * @return string
     */
    protected function render($template) {
        $parser = $this->getInstance();

        foreach ($this->all() as $key => $value) {
            $parser->assign($key, $value);
        }

        return $parser->fetch($template);
    }

    /**
     * Creates new Smarty object instance if it does not already exist, and returns it
     *
     * @throws ErrorException If Smarty lib directory does not exist
     * @return Smarty
     */
    public function getInstance() {
        if ($this->parserInstance instanceof Smarty) {
            return $this->parserInstance;
        }

        $this->parserInstance = new Smarty();
        $this->parserInstance->addTemplateDir($this->getTemplatesDirectory());

        if (isset($this->settings['caching'])) {
            $this->parserInstance->caching = $this->settings['caching'];
        }

        if (isset($this->settings['plugins_dir'])) {
            $this->settings['plugins_dir'] = rtrim($this->settings['plugins_dir'], '/\\');
            $this->parserInstance->addPluginsDir($this->settings['plugins_dir']);

            foreach (glob($this->settings['plugins_dir'] . '/*.php') as $plugin) {
                $fileName = pathinfo($plugin, PATHINFO_FILENAME);
                list($type, $name) = explode('.', $fileName);
                $pluginFunctionName = 'smarty_' . $type . '_' . $name;
                $this->parserInstance->loadPlugin($pluginFunctionName);
                $this->parserInstance->registerPlugin($type, $name, $pluginFunctionName);
                if (preg_match('/(pre|post|output)filter\./i', $fileName, $match)) {
                    $filterType = $match[1];
                    $this->parserInstance->registerFilter($filterType, $pluginFunctionName);
                }
                //var_dump($this->parserInstance->registered_plugins);
            }
        }

        if (isset($this->settings['compile_dir'])) {
            $this->parserInstance->setCompileDir($this->settings['compile_dir']);
        }

        if (isset($this->settings['cache_dir'])) {
            $this->parserInstance->setCacheDir($this->settings['cache_dir']);
        }

        if (isset($this->settings['left_delimiter'])) {
            $this->parserInstance->setLeftDelimiter($this->settings['left_delimiter']);
        }

        if (isset($this->settings['right_delimiter'])) {
            $this->parserInstance->setRightDelimiter($this->settings['right_delimiter']);
        }

        if (config('debug')) {
            $this->parserInstance->setDebugging(true);
        }

        return $this->parserInstance;
    }

}
